package cn.itsource.pethome.web.controller;

import cn.itsource.pethome.domain.Shop;
import cn.itsource.pethome.domain.dto.UserDto;
import cn.itsource.pethome.service.IShopService;
import cn.itsource.pethome.service.IUserService;
import cn.itsource.pethome.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/lr")
public class LoginAndRegisterController {

    @Autowired
    private IShopService shopService;
    @Autowired
    private IUserService userService;

    /**
     * 商家入驻表单提交
     * @param shop
     * @return
     */
    @PostMapping("/settledIn")
    public AjaxResult settledIn(@RequestBody Shop shop){
        try {
            shopService.settledIn(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("入驻失败！" + e.getMessage());
        }
    }

    /**
     * 商家用户激活
     * @param id
     */
    @GetMapping("/active/{id}")
    public void active(@PathVariable("id")Long id, HttpServletResponse response){
        try {
            //调用Service层的激活方法

            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("<script type='text/javascript'>alert('激活成功！');location.href='http://localhost:8088/#/login';</script>");
        } catch (IOException e) {
            e.printStackTrace();
            try {
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().write("<script type='text/javascript'>alert('激活失败！');</script>");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    /**
     * 验证手机号唯一
     * @param type phoneReg表示手机验证码注册 phoneLogin表示手机验证码登录
     * @param phone 手机号
     * @return
     */
    @GetMapping("/checkPhone/{type}/{phone}")
    public AjaxResult checkPhone(@PathVariable("type") String type, @PathVariable("phone") String phone){
        return userService.checkPhone(type, phone);
    }

    /**
     * 发送手机验证码
     * @param type phoneReg表示手机验证码注册 phoneLogin表示手机验证码登录
     * @param phone 手机号
     * @return
     */
    @GetMapping("/sendMobileCode/{type}/{phone}")
    public AjaxResult sendMobileCode(@PathVariable("type") String type, @PathVariable("phone") String phone){
        return userService.sendMobileCode(type, phone);
    }

    /**
     * 用户注册
     * 要接收前端提交过来的参数，尤其是有一个verifyCode
     * 办法：
     *  1）直接在User类中添加verifyCode字段
     *  2）写一个User的子类dto，在子类中写verifyCode字段
     *  3）可以使用Map集合接收，再使用BeanUtils的copyProperties方法复制对象属性
     *
     *  dto：data transfer object 数据传输对象
     * @param userDto
     * @return
     */
    @PutMapping("/userReg")
    public AjaxResult userReg(@RequestBody UserDto userDto){
        try {
            return userService.userReg(userDto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("注册失败！" + e.getMessage());
        }
    }


    /**
     * 用户登录
     * userDto的type属性：
     *  front表示前台普通用户登录
     *  admin表示后台管理系统用户登录
     * @param userDto
     * @return
     */
    @PostMapping("/passwordLogin")
    public AjaxResult passwordLogin(@RequestBody UserDto userDto, HttpSession session){
        try {
            return userService.login(userDto,session);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙！" + e.getMessage());
        }
    }

    /**
     * 微信登录：通过code获取AccessToken
     * @param code
     * @return
     */
    @GetMapping("/wechatLogin/{code}")
    public AjaxResult wechatLogin(@PathVariable("code")String code){
        System.out.println("wechatLogin ====> " + code);
        return userService.wechatLogin(code);
    }


    /**
     * 微信用户绑定
     * @param userDto type等于phoneBind表示手机验证码绑定 usernameBind表示用户名密码绑定
     * @return
     */
    @PostMapping("/wechatUserBind")
    public AjaxResult wechatUserBind(@RequestBody UserDto userDto){
        return userService.wechatUserBind(userDto);
    }


}
